package com.foreknow.tijian.service.Impl;

import com.foreknow.tijian.dao.Impl.OrdersDaoImpl;
import com.foreknow.tijian.dao.ordersDao;
import com.foreknow.tijian.po.Orders;
import com.foreknow.tijian.service.OrdersService;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    //建立连接
    Connection conn= DBUtil.getConnection();
    PreparedStatement pst;
    @Override
    public int getOrdersByUserId(Orders order) {
        int state =Integer.MAX_VALUE;
        ordersDao ordersDao = new OrdersDaoImpl();
        try{
            //调用Dao层的方法
           state=ordersDao.getOrdersByUserId(order);
        }
        catch(Exception e){
        }
        finally {
            //关闭资源
            DBUtil.close(pst);
        }
        return state;

    }

    @Override
    public int saveOrders(Orders order) {
        int result=0;
        ordersDao ordersDao = new OrdersDaoImpl();
        try {
            //开始事务
            DBUtil.beginTransaction();
            result = ordersDao.saveOrders(order);
            //提交事务
            DBUtil.commitTransaction();
        }  catch (Exception e) {
            try {
                //取消之前的所有操作
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int removeOrders(Orders order) {
        int result=0;
        ordersDao ordersDao = new OrdersDaoImpl();
        try {
            //开始事务
            DBUtil.beginTransaction();
           result=ordersDao.removeOrders(order);
            //提交事务
            DBUtil.commitTransaction();
        }  catch (Exception e) {
            try {
                //取消之前的所有操作
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Orders order) {
        Orders orders=new Orders();
        ordersDao ordersDao = new OrdersDaoImpl();
        try{
            //调用Dao层的方法
          orders=ordersDao.getOrdersById(order);
        }
        catch(Exception e){
        }
        finally {
            //关闭资源
            DBUtil.close(pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(Orders order) {
        List<Orders> list=new ArrayList<>();
        ordersDao ordersDao = new OrdersDaoImpl();
        try{
            //调用Dao层的方法
            list=ordersDao.listOrdersByUserId(order);
        }
        catch(Exception e){
        }
        finally {
            //关闭资源
            DBUtil.close(pst);
        }
        return list;


    }
}
